package top.lywivan.mm.controller;

import top.lywivan.mm.common.GlobalConst;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.entity.Result;
import top.lywivan.mm.pojo.Course;
import top.lywivan.mm.pojo.User;
import top.lywivan.mm.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @PostMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = courseService.findListByPage(queryPageBean);
        return new Result(true,"分页查询成功",pageResult);
    }

    @PostMapping("/add")
    public Result addCourse(@RequestBody Course course, HttpSession session){
        User user = (User) session.getAttribute(GlobalConst.SESSION_KEY_USER);
        if (user==null){
            user=new User();
            user.setId(1);
        }
        course.setUserId(user.getId());
        courseService.addCourse(course);
        return new Result(true,"添加成功");
    }

    @PostMapping("/update")
    public Result updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return new Result(true,"修改成功");
    }

    @GetMapping("/delete")
    public Result deleteCourseById(Integer courseId){
        courseService.deleteCourseById(courseId);
        return new Result(true,"删除成功");
    }

    @GetMapping("/findListAll")
    public Result findListAllCourse(){
        List<Course> list = courseService.findListAllCourse();
        return new Result(true,"查询全部学科成功",list);
    }
}
